package com.huanghuai.retail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanghuai.retail.common.R;
import com.huanghuai.retail.dto.CargoDto;
import com.huanghuai.retail.entity.CargoLanes;
import com.huanghuai.retail.entity.Device;
import com.huanghuai.retail.entity.DeviceType;
import com.huanghuai.retail.entity.PInformation;
import com.huanghuai.retail.mapper.CargoLanesMapper;
import com.huanghuai.retail.service.CargoLanesService;
import com.huanghuai.retail.service.DeviceService;
import com.huanghuai.retail.service.DeviceTypeService;
import com.huanghuai.retail.service.InformationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CargoLanesServiceImpl  extends ServiceImpl<CargoLanesMapper, CargoLanes> implements CargoLanesService {
    @Resource
    private InformationService informationService;
    @Resource
    private DeviceService deviceService;
    @Resource
    private DeviceTypeService deviceTypeService;
    @Override
    public List<CargoLanes> findDeviceId(Long deviceId) {
        LambdaQueryWrapper<CargoLanes> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(deviceId!=null,CargoLanes::getDeviceId, deviceId);
        List<CargoLanes> cargoLanes = list(wrapper);
        if (cargoLanes != null && cargoLanes.size() > 0) {
            return cargoLanes;
        }
        return null;

    }

    @Override
    public R<String> setCargo(CargoDto cargoDto) {
        Long deviceId = cargoDto.getDeviceId();

        Long[][] cargo = cargoDto.getCargo();
        if (deviceId == null || cargo == null) {
            return R.error("数据缺失");
        }
        if (cargo.length == 0) {
            return R.error("缺少货道信息");
        }
        //ids 占用多个货道的商品id;
        Long ids = 0L;
        //flag 占用货道数
        int flag = 0;
        //c 查询原有设备货道摆放
        List<CargoLanes> C = findDeviceId(deviceId);
//        CargoLanes cargoL =findId(C,1,1);
//        if(C==null){
//            return R.error("未找到相关货道信息");
//        }
        for (int i = 0; i < cargo.length; i++) {
            Long[] card = cargo[i];
//            CargoLanes cargoLanes1 = new CargoLanes();
            for (int j = 0; j < card.length; j++) {
                Long car = cargo[i][j];
                CargoLanes cargoLanes = new CargoLanes();
                if (C != null) {
                    cargoLanes = findId(C, i, j);
                }
                cargoLanes.setDeviceId(deviceId);
                cargoLanes.setRowX(i);
                cargoLanes.setColumnY(j);
                cargoLanes.setCommodityId(car);
                if (flag > 1) {
                    cargoLanes.setStatus(1);
                    flag--;
                }else cargoLanes.setStatus(0);
                if (C != null) {
                    updateById(cargoLanes);
                } else save(cargoLanes);
                if (car == 0) {
                    continue;
                }
                PInformation sp = informationService.getById(car);
                if (sp.getLanes() != 1) {
                    flag = sp.getLanes();
                }
            }
        }
        return R.success("添加成功");
    }

    @Override
    public R<CargoDto> getCargoById(Long id) {
        if (id == null||id<=0) {
            return R.error("参数错误");
        }

        List<CargoLanes> C = findDeviceId(id);
        CargoDto cargoDto = new CargoDto();
        cargoDto.setDeviceId(id);
        Device device = deviceService.getById(id);
        DeviceType deviceType=deviceTypeService.getById(device.getDeviceTypeId());
        cargoDto.setColumnY(deviceType.getColumnY());
        cargoDto.setRowX(deviceType.getRowX());
        PInformation[][] info=new PInformation[deviceType.getRowX()][deviceType.getColumnY()];
        for (CargoLanes cargoLanes: C) {
            Integer x=cargoLanes.getRowX();
            Integer y=cargoLanes.getColumnY();
            Long commodityId = cargoLanes.getCommodityId();
            if (commodityId == null){
                continue;
            }
            info[x][y] = informationService.getById(commodityId);
        }
        cargoDto.setInformations(info);
        return R.success(cargoDto);

    }

    private CargoLanes findId(List<CargoLanes> c, int i, int i1) {
        CargoLanes cargoL = null;
        for (CargoLanes tmp : c) {
            if (tmp.getRowX() == i && tmp.getColumnY() == i1)
                cargoL = tmp;
        }
        return cargoL;
    }
}
